package com.wydexcel.generate.process;

import com.wydexcel.generate.properties.s.ExcelFontProperties;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFColor;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

public class WydExcelCellFontProcessImpl implements WydExcelCellFontProcess {

    private final Map<String, Font> map = new HashMap<>();

    private String getKey(String fieldName, boolean isBody, ExcelFontProperties property) {
        String key = fieldName;
        int isHeader = property.getIsHeader();
        if (isHeader == 1) {
            key += isBody;
            return key;
        }

        if (isHeader == 2 && isBody) {
            key += isBody;
            return key;
        }
        key += "0";
        return key;
    }

    @Override
    public Font fontProcess(Row row, Cell cell, ExcelFontProperties property, String fieldName, boolean isBody) {
        try {
            if (null == property) {
                return null;
            }
            if (isBody && property.getIsHeader() == 1) {
                return null;
            }
            if (!isBody && property.getIsHeader() == 2) {
                return null;
            }
            String key = getKey(fieldName, isBody, property);
            Workbook workbook = row.getSheet().getWorkbook();
            if (null == map.get(key)) {
                Font font = row.getSheet().getWorkbook().createFont();
                Optional.of(property).ifPresent(excelFontDto -> {
                    Optional.ofNullable(property.getFontHeight()).ifPresent(font::setFontHeight);
                    Optional.of(property.isBold()).ifPresent(font::setBold);
                    if (property.getRed() == null && null == property.getGreen() && null == property.getBlue()) {
                        Optional.of(property.getFontColor()).ifPresent(font::setColor);
                    } else {
                        if (workbook instanceof XSSFWorkbook) {
                            XSSFFont xssfFont = (XSSFFont) font;
                            XSSFWorkbook workbook1 = (XSSFWorkbook) workbook;
                            xssfFont.setColor(new XSSFColor(new java.awt.Color(property.getRed(), property.getGreen(), property.getBlue()), workbook1.getStylesSource().getIndexedColors()));
                        }
                    }

                    Optional.ofNullable(property.getFontName()).ifPresent(font::setFontName);
                    Optional.of(property.isStrikeout()).ifPresent(font::setStrikeout);
                    Optional.of(property.isItalic()).ifPresent(font::setItalic);
//                    Optional.ofNullable(property.getFontColor()).ifPresent(font::setF);
//                    font.setColor(property.getFontColor());
                    if (null != property.getFontHeightPoints()) {
                        font.setFontHeightInPoints(property.getFontHeightPoints());
                    }
                    if (null != property.getUnderLine()) {
                        font.setUnderline(property.getUnderLine());
                    }
                    map.put(key, font);
                });
                return font;
            }
            return map.get(key);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }


    }

}
